//
//  ForgetPwdVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/6/30.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ForgetPwdVC.h"

@interface ForgetPwdVC ()

@property (nonatomic, strong) UITextField *companyField;
@property (nonatomic, strong) UITextField *phoneField;
@property (nonatomic, strong) UITextField *codeField;
@property (nonatomic, strong) UIButton *getCodeBtn;
@property (nonatomic, strong) UITextField *pwdField;
@property (nonatomic, strong) UIButton *showBtn;
@property (nonatomic, strong) UIButton *commitBtn;

@end

@implementation ForgetPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
    [self configUI];
}

- (void)configUI {
    self.view.backgroundColor = HexColor(0xffffff);
    
    UILabel *companyTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, ScreenW - 80, 13)];
    companyTitleLab.text = @"公司编号";
    companyTitleLab.font = [UIFont systemFontOfSize:12];
    companyTitleLab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:companyTitleLab];
    
    self.companyField = [[UITextField alloc] initWithFrame:CGRectMake(40, companyTitleLab.nim_bottom + 2, companyTitleLab.nim_width, 40)];
    self.companyField.placeholder = @"请输入公司编号";
    self.companyField.font = MediumFontOfSize(16);
    self.companyField.textColor = HexColor(0x2B2C44);
    [self.view addSubview:self.companyField];
    
    UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.companyField.nim_bottom, self.companyField.nim_width, 1)];
    line1.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:line1];
    
    UILabel *phoneTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, line1.nim_bottom + 20, ScreenW - 80, 13)];
    phoneTitleLab.text = @"手机号";
    phoneTitleLab.font = [UIFont systemFontOfSize:12];
    phoneTitleLab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:phoneTitleLab];
    
    self.phoneField = [[UITextField alloc] initWithFrame:CGRectMake(40, phoneTitleLab.nim_bottom + 2, phoneTitleLab.nim_width, 40)];
    self.phoneField.placeholder = @"请输入手机号";
    self.phoneField.font = MediumFontOfSize(16);
    self.phoneField.textColor = HexColor(0x2B2C44);
    [self.view addSubview:self.phoneField];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, self.phoneField.nim_height)];
    UILabel *phoneLeftLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, self.phoneField.nim_height-2)];
    phoneLeftLab.text = @"+86";
    phoneLeftLab.textColor = HexColor(0x2B2C44);
    phoneLeftLab.font = MediumFontOfSize(16);
    [leftView addSubview:phoneLeftLab];
    self.phoneField.leftView = leftView;
    self.phoneField.leftViewMode = UITextFieldViewModeAlways;
    
    UILabel *line3 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.phoneField.nim_bottom, self.phoneField.nim_width, 1)];
    line3.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:line3];
    
    UILabel *codeTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, line3.nim_bottom + 20, phoneTitleLab.nim_width, 13)];
    codeTitleLab.text = @"短信验证码";
    codeTitleLab.font = [UIFont systemFontOfSize:12];
    codeTitleLab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:codeTitleLab];
    
    self.codeField = [[UITextField alloc] initWithFrame:CGRectMake(40, codeTitleLab.nim_bottom + 2, codeTitleLab.nim_width, 40)];
    self.codeField.placeholder = @"请输入验证码";
    self.codeField.font = MediumFontOfSize(16);
    self.codeField.textColor = HexColor(0x2B2C44);
    [self.view addSubview:self.codeField];
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 102, 40)];
    self.getCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 102, 30)];
    self.getCodeBtn.layer.cornerRadius = 15;
    [self.getCodeBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
    [self.getCodeBtn setTitleColor:HexColor(0x3D68EB) forState:0];
    [self.getCodeBtn setTitle:@"获取验证码" forState:0];
    self.getCodeBtn.titleLabel.font = BoldFontOfSize(14);
    [self.getCodeBtn addTarget:self action:@selector(getCodeClick) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:self.getCodeBtn];
    self.codeField.rightView = rightView;
    self.codeField.rightViewMode = UITextFieldViewModeAlways;
    
    UILabel *line4 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.codeField.nim_bottom, self.codeField.nim_width, 1)];
    line4.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:line4];
    
    UILabel *pwdTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(40, line4.nim_bottom + 20, phoneTitleLab.nim_width, 13)];
    pwdTitleLab.text = @"密码";
    pwdTitleLab.font = [UIFont systemFontOfSize:12];
    pwdTitleLab.textColor = HexColor(0x2B2C44);
    [self.view addSubview:pwdTitleLab];
    
    self.pwdField = [[UITextField alloc] initWithFrame:CGRectMake(40, pwdTitleLab.nim_bottom + 2, pwdTitleLab.nim_width, 40)];
    self.pwdField.placeholder = @"请输入密码";
    self.pwdField.font = MediumFontOfSize(16);
    self.pwdField.textColor = HexColor(0x2B2C44);
    self.pwdField.secureTextEntry = YES;
    [self.view addSubview:self.pwdField];
    
    self.showBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
    [self.showBtn setImage:[UIImage imageNamed:@"pwd_show"] forState:0];
    [self.showBtn addTarget:self action:@selector(showBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.pwdField.rightView = self.showBtn;
    self.pwdField.rightViewMode = UITextFieldViewModeAlways;
    
    UILabel *line5 = [[UILabel alloc] initWithFrame:CGRectMake(40, self.pwdField.nim_bottom, self.pwdField.nim_width, 1)];
    line5.backgroundColor = HexColor(0xF2F2F5);
    [self.view addSubview:line5];
    
    self.commitBtn = [[UIButton alloc] initWithFrame:CGRectMake(40, line5.nim_bottom + 30, ScreenW - 80, 44)];
    [self.commitBtn setTitle:@"提交" forState:0];
    [self.commitBtn setTitleColor:HexColor(0xffffff) forState:0];
    self.commitBtn.titleLabel.font = BoldFontOfSize(16);
    [self.commitBtn setBackgroundColor:HexColor(0x3D68EB)];
    self.commitBtn.layer.masksToBounds = YES;
    self.commitBtn.layer.cornerRadius = 6;
    [self.commitBtn addTarget:self action:@selector(commitClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.commitBtn];
}

- (void)showBtnClick {
    self.pwdField.secureTextEntry = !self.pwdField.isSecureTextEntry;
    [self.showBtn setImage:[UIImage imageNamed:self.pwdField.isSecureTextEntry ? @"pwd_show" : @"pwd_hide"] forState:0];
}

- (void)commitClick {
    if ([FactoryMethod isNull:self.companyField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入公司编号"];
        return;
    }
    if ([FactoryMethod isNull:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入手机号"];
        return;
    }
    if (![FactoryMethod isPhone:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入正确的手机号"];
        return;
    }
    if ([FactoryMethod isNull:self.codeField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入验证码"];
        return;
    }
    if ([FactoryMethod isNull:self.pwdField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入密码"];
        return;
    }
    [MBProgressHUD leShowHUDWithMessage:@"提交中" activity:YES];
    [HttpRequest PostWithApi:LogisticsSupplierForgetPwd params:@{@"company_no":self.companyField.text,@"phone":self.phoneField.text,@"code":self.codeField.text,@"password":self.pwdField.text} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            __weak_self;
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"修改成功，请登录" delayOperation:^{
                [weakself.rt_navigationController popViewControllerAnimated:YES];
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)getCodeClick {
    if ([FactoryMethod isNull:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入手机号"];
        return;
    }
    if (![FactoryMethod isPhone:self.phoneField.text]) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入正确的手机号"];
        return;
    }
    [self timeStart];
    [self requestForGetCode];
}

- (void)requestForGetCode {
    [MBProgressHUD leShowHUDWithMessage:@"请求中" activity:YES];
    [HttpRequest PostWithApi:LogisticsSupplierGetRegistCode params:@{@"phone":self.phoneField.text} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"验证码获取成功"];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)timeStart{
    [self.codeField becomeFirstResponder];
    self.getCodeBtn.userInteractionEnabled = NO;
    self.codeField.text = @"";
    __block int timeout = 60; //倒计时时间
    /**
     *  gcd异步处理倒计时控件
     */
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.getCodeBtn.userInteractionEnabled = YES;
                [self.getCodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
                [self.getCodeBtn setTitleColor:HexColor(0x3D68EB) forState:UIControlStateNormal];
            });
        }else{
            int seconds = timeout;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.getCodeBtn.userInteractionEnabled = NO;
                NSString *strTime = [NSString stringWithFormat:@"%d S",seconds];
                [self.getCodeBtn setTitle:strTime forState:(UIControlStateNormal)];
                [self.getCodeBtn setTitleColor:HexColor(0x2B2C44) forState:UIControlStateNormal];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

@end
